package com.burgeon.backend.EnterpriseWeChat.rest;

import com.burgeon.backend.EnterpriseWeChat.entity.SaleStoreData;
import com.burgeon.backend.EnterpriseWeChat.entity.StoreData;
import com.burgeon.backend.Rest.base.RESTBase;
import org.json.JSONArray;
import org.springframework.util.MultiValueMap;

import java.util.ArrayList;
import java.util.HashMap;

public class FindStoreData extends RESTBase {

    private String ip;
    private String account;
    private String password;

    public FindStoreData(String ip, String account, String password) {
        this.ip = ip;
        this.account = account;
        this.password = password;
    }

    /**
     * 发送并接收字符创
     *
     * @return
     */

    public StoreData getData(String phone) throws Exception {
        String url = "http://" + ip + "/servlets/binserv/Rest";

        String str = RESULT(Data(phone), url);
//        System.err.println(str);
        String code = new JSONArray(str).getJSONObject(0).getString("code");
        if (!"0".equals(code)) {
            throw new Exception(new JSONArray(str).getJSONObject(0).getString("messsage"));
        }

        JSONArray jsonArray = new JSONArray(str).getJSONObject(0).getJSONArray("rows");
        StoreData saleStoreData = new StoreData();
        if(jsonArray.length()>0){

            saleStoreData.setStoreName(jsonArray.getJSONArray(0).getString(0));
            saleStoreData.setTotAmtPrice(jsonArray.getJSONArray(0).getString(1));
            saleStoreData.setnVipStore(jsonArray.getJSONArray(0).getString(2));
            saleStoreData.setStoreTB(jsonArray.getJSONArray(0).getString(3));
            saleStoreData.setStoreHB(jsonArray.getJSONArray(0).getString(4));
            saleStoreData.setStoreRATE(jsonArray.getJSONArray(0).getString(5));
            saleStoreData.setTotAmtMark(jsonArray.getJSONArray(0).getString(6));

            saleStoreData.setTotVipStoreQty(jsonArray.getJSONArray(0).getString(7));
            saleStoreData.setTotAmtVipPrice(jsonArray.getJSONArray(0).getString(8));
            saleStoreData.setVipMoney(jsonArray.getJSONArray(0).getString(9));
            saleStoreData.setStoreId(jsonArray.getJSONArray(0).getString(10));


        }

        return saleStoreData;
    }

    private MultiValueMap Data(String phone) {
        HashMap params = new HashMap();
        ArrayList colums = new ArrayList();//查询字段


        colums.add("'C_STORE_ID;NAME'");//店仓
        colums.add("TOT_AMT_PRICE");//店铺销售额
        colums.add("N_VIPSTORE");//店铺新增会员数
        colums.add("STORE_TB");//店铺同比
        colums.add("STORE_HB");//店铺环比
        colums.add("STORE_RATE");//店铺达成率
        colums.add("TOT_AMT_MARK");//店铺指标


        colums.add("TOT_VIPSTORE_QTY");//店铺总会员数
        colums.add("TOT_AMTVIP_PRICE");//店铺会销
        colums.add("VIP_MONEY");//店铺储值金额

        colums.add("C_STORE_ID");//店仓


        params.put("columns", colums);
        params.put("table", "S_BULLETINBOARD");//表名

        params.put("count", "true");

        HashMap paramss = new HashMap();
        paramss.put("column", "'PHONE'");
        paramss.put("condition", "'="+phone+"'");

        params.put("params", paramss);

        return DATA("Query", params, account, password);
    }

    public static void main(String[] args) {
        FindStoreData g = new FindStoreData("59.110.28.94:90", "nea@burgeon.com.cn", "burgeon55321");
        try {
            String phone = null;
//            =======EWEmp
//            '13832948581
            System.err.println(g.getData(null));
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}
